#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
/*
 * Copyright © 2014, Finium Sdn Bhd, All Rights Reserved
 * 
 * AbstractHibernateUtil.java
 * Modification History
 * *************************************************************
 * Date				Author						Comment
 * Oct 26, 2013		Venkaiah Chowdary Koneru	Created
 * *************************************************************
 */
package ${package}.support.hibernate;

import ${package}.dto.ListProp;

/**
 * Utility methods related to Generic Hibernate functionality.
 * 
 * @author Venkaiah Chowdary Koneru
 */
public abstract class AbstractHibernateUtil
{
    private static final long START_INDEX = -2;

    /**
     * Updates Start Index and End Index values of List prop object. These
     * values will then be used in HQL/Criteria/Named Queries for pagination
     * purposes.
     * 
     * @param listProp
     *            must not be null
     * @return true - if pagination is updated. false - if pagination is not
     *         updated.
     */
    protected static boolean updatePagination(ListProp listProp)
    {
        if (listProp.getStartIndex() > -1)
        {
            if (listProp.getTotalRecords() == -1)
            {
                throw new IllegalStateException("Total records not set.");
            }

            if (listProp.getTotalRecords() == 0) // no record
            {
                listProp.setStartIndex(-1);
                listProp.setEndIndex(-1);
            }
            else
            {
                if (listProp.getStartIndex() >= listProp.getTotalRecords())
                {
                    listProp.setStartIndex(listProp.getTotalRecords() - 1);
                }
                if (listProp.getEndIndex() >= listProp.getTotalRecords())
                {
                    listProp.setEndIndex(listProp.getTotalRecords() - 1);
                }
            }

            return true;
        }

        return false;
    }

    /**
     * returns the default start index for pagination.
     * 
     * @return start index value (-2).
     */
    protected static long getStartIndex()
    {
        return START_INDEX;
    }
}
